import numpy as np

# 1.阶跃函数

def step_function(x):
    if x > 0:
        return 1
    else:
        return 0

# 传入向量或者矩阵
def step_function_v2(x):
    return np.array(x > 0, dtype=int)

# 2. sigmoid 函数
def sigmoid_function(x):
    return 1/(1 + np.exp(-x))

# 3. relu 函数
def relu_function(x):
    return np.maximum(0, x)


# 4 softmax 函数

# x 为向量
def softmax_function(x):
    x = x-np.max(x)
    return np.exp(x) / np.sum(np.exp(x))

def softmax_function_v2(x):
    if x.ndim == 2:
        x = x.T
        x = x - np.max(x,axis=0)
        y = np.exp(x) / np.sum(np.exp(x),axis=0)
        return y.T

    x = x - np.max(x)
    return np.exp(x) / np.sum(np.exp(x))


# 5. 恒等函数

def identity(x):
    return x






if __name__ == '__main__':
    data = [10,20,30,-1,-2,-3]

    # print(step_function(step_function(data)))
    print(step_function_v2(np.array(data)))

    print(sigmoid_function(10))
    print(softmax_function(np.array(data)))